package com.haha.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.haha.gulimall.product.entity.ProductAttrValueEntity;
import com.haha.gulimall.product.service.ProductAttrValueService;
import com.haha.gulimall.product.vo.AttrResponseVo;
import com.haha.gulimall.product.vo.AttrVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.haha.gulimall.product.service.AttrService;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.R;



/**
 * 商品属性
 *
 * @author xiongweizhong
 * @email xiongwei@gmail.com
 * @date 2022-06-22 23:36:06
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;

    //http://localhost:88/api/product/attr/base/listforspu/17
    @RequestMapping("/base/listforspu/{spuId}")
    //@RequiresPermissions("product:attr:list")
    public R attrValueForSpu(@PathVariable("spuId") Long spuId){
List<ProductAttrValueEntity> entities =productAttrValueService.attrValueForSpu(spuId);

        return R.ok().put("data", entities);
    }

    @RequestMapping("/update/{spuId}")
    //@RequiresPermissions("product:attr:list")
    public R updateAttrValueForSpu(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
       productAttrValueService.updateAttrValueForSpu(spuId,entities);

        return R.ok().put("data", entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    ///product/attr/sale/list/{catelogId}
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable Long catelogId
    ,@PathVariable("attrType") String type){
        PageUtils page = attrService.getBaseAttrList(params,catelogId,type);
        return R.ok().put("page",page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResponseVo attrResponseVo =attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrResponseVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrResponseVo attrResponseVo){
		attrService.updateAttr(attrResponseVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
